/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monkify;

/**
 *
 * @author pierre
 */
public class Util {
  /** 
  * http://stackoverflow.com/questions/8545590/java-find-the-most-popular-element-in-int-array
  */
  static int getPopularElement(Integer[] a) {
    int count = 1, tempCount;
    int popular = a[0];
    int temp = 0;
    for (int i = 0; i < (a.length - 1); i++) {
      temp = a[i];
      tempCount = 0;
      for (int j = 1; j < a.length; j++) {
        if (temp == a[j])
          tempCount++;
      }
      if (tempCount > count) {
        popular = temp;
        count = tempCount;
      }
    }
    return popular;
  }

  static int toDecade(int year) {
    return (year % 100) / 10;
  }

  static boolean isURI(String s) {
    return s.length() == 36 && s.substring(0, 14).equals("spotify:track:");
  }
  
  static String concatArray(String[] ss) {
      StringBuilder sb = new StringBuilder();
      
      for(String s : ss) {
          sb.append(s);
      }
      
      return sb.toString();
  }
  
  static String[] pierreLevel(String[][] level, String start, String goal) {
     String[] result = new String[level.length+1];
     
     result[0] = "Reimertz," + level[0].length + "," + level.length;
     
     for(int i = 0; i < level.length; i++) {
         String[] shadow = level[i].clone();
         
         for(int j = 0; j < shadow.length; j++) {
             shadow[j] = shadow[j].equals(start)    ? "S" : shadow[j];
             shadow[j] = shadow[j].startsWith(goal) ? "X" : shadow[j];
             shadow[j] = shadow[j].equals("_")      ? " " : shadow[j];
             shadow[j] = shadow[j].startsWith("M")  ? " " : shadow[j];
             shadow[j] = shadow[j].equals("U")      ? "#" : shadow[j];
         }
         
         result[i+1] = Util.concatArray(shadow);
     } 
     
     return result;
  }
}
